﻿using BCCommon;
using BCCommon.Enums;
using BCEntity.Website.User;
using BCEntity.WeChatComponent.WeChatUserInfo;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace BCData.Website.User.User
{
    public class UserData : IUserData
    {
        protected readonly IDatabaseContext databaseContext;

        private const string QUERY = @"SELECT A.UserId,A.Phone,A.Password,A.UserName,A.CreateTime,A.LastLoginTime,A.CurrentlyLoginTime,A.IdentityCardNo,A.UserIntegrals,
                                                A.LogonIp,A.AddressIp,A.Avatar,A.AccountStatus,A.ProvinceId,A.ProvinceCode,A.CityId,A.CityCode,A.CountyId,A.CountyCode,A.Role,A.PassCode,A.PassExpirationTime,A.UserLevel,
                                                A.IsVip,A.IsEnterprise,A.CompanyName,A.UniqueCode,A.PartnerId,A.EvaluationScore,C.Unionid,D.ProvinceName,E.CityName,F.CountyName,A.IsReceiveRedPacket,
                                                A.IdentityCardPath,A.IdentityCardBackPath,A.RealName,A.RealNameVerifiesStatus,A.RealNameVerifiesRefuse,A.ApproveTime,A.Approver,A.ApproverId,A.ParentId 
                                        FROM User A
                                            LEFT JOIN UserAuth B ON A.UserId = B.UserId
                                            LEFT JOIN WeChatUserInfo C ON C.Unionid=B.Unionid
                                            LEFT JOIN Province D ON A.ProvinceId=D.ProvinceId
                                            LEFT JOIN City E ON A.CityId=E.CityId
                                            LEFT JOIN County F ON A.CountyId=F.CountyId";

        private const string QUERY_UNIONID_VIEW = @"SELECT A.UserId,A.Phone,A.Password,A.UserName,A.CreateTime,A.LastLoginTime,A.CurrentlyLoginTime,A.IdentityCardNo,A.UserIntegrals,
                                                A.LogonIp,A.AddressIp,A.Avatar,A.AccountStatus,A.ProvinceId,A.ProvinceCode,A.CityId,A.CityCode,A.CountyId,A.CountyCode,A.Role,A.PassCode,A.PassExpirationTime,A.UserLevel,C.Nickname,
                                                C.Sex,C.Headimgurl,C.Remark,C.SubscribeTime,C.Unionid,A.IsVip,A.IsEnterprise,A.CompanyName,A.UniqueCode,A.PartnerId,A.EvaluationScore,
                                                D.ProvinceName,E.CityName,F.CountyName,A.IsReceiveRedPacket 
                                            FROM User A
                                            LEFT JOIN UserAuth B ON A.UserId = B.UserId
                                            LEFT JOIN WeChatUserInfo C ON C.Unionid=B.Unionid
                                            LEFT JOIN Province D ON D.ProvinceId = A.ProvinceId
                                            LEFT JOIN City E ON E.CityId =A.CityId
                                            LEFT JOIN County F ON F.CountyId = A.CountyId";

        public UserData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<bool> CheckUserExistsByPhone(string phone)
        {
            string sql = @"SELECT 1 FROM User WHERE Phone=?Phone";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?Phone", phone, DbType.AnsiString, size: 20);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameter) != null;
        }

        public async Task<UserEntity> Create(UserEntity entity)
        {
            string sql = @"INSERT INTO User(Phone,Password,UserName,IdentityCardNo,Avatar,AccountStatus,ProvinceCode,CityCode,CountyCode,Role,PassCode,PassExpirationTime,LogonIp,AddressIp,UniqueCode,PartnerId,IsReceiveRedPacket)
                                VALUES(?Phone,?Password,?UserName,?IdentityCardNo,?Avatar,?AccountStatus,?ProvinceCode,?CityCode,?CountyCode,?Role,?PassCode,?PassExpirationTime,?LogonIp,?AddressIp,UUID_SHORT(),?PartnerId,?IsReceiveRedPacket);
                            {0} WHERE A.UserId=LAST_INSERT_ID();";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?Phone", entity.Phone, DbType.AnsiString, size: 20);
            parameter.Add("?Password", entity.Password, DbType.AnsiString, size: 255);
            parameter.Add("?UserName", entity.UserName, DbType.AnsiString, size: 20);
            parameter.Add("?IdentityCardNo", entity.IdentityCardNo, DbType.AnsiString, size: 18);
            parameter.Add("?Avatar", entity.Avatar, DbType.AnsiString, size: 255);
            parameter.Add("?AccountStatus", entity.AccountStatus.ToString(), DbType.AnsiString);
            parameter.Add("?ProvinceCode", entity.ProvinceCode, DbType.AnsiString, size: 20);
            parameter.Add("?CityCode", entity.CityCode, DbType.AnsiString, size: 20);
            parameter.Add("?CountyCode", entity.CountyCode, DbType.AnsiString, size: 20);
            parameter.Add("?Role", entity.Role, DbType.AnsiString, size: 30);
            parameter.Add("?PassCode", entity.PassCode, DbType.AnsiString, size: 10);
            parameter.Add("?PassExpirationTime", entity.PassExpirationTime, DbType.DateTime);
            parameter.Add("?LogonIp", entity.LogonIp, DbType.AnsiString, size: 50);
            parameter.Add("?AddressIp", entity.AddressIp, DbType.AnsiString, size: 50);
            parameter.Add("?PartnerId", entity.PartnerId, DbType.Int64, size: 20);
            parameter.Add("?IsReceiveRedPacket", entity.IsReceiveRedPacket, DbType.Boolean);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<UserEntity>(string.Format(sql, QUERY), parameter, databaseContext.Transaction);
        }
        public async Task<UserEntity> GetUserByUniqueCode(string uniqueCode)
        {
            string sql = string.Concat(QUERY, " WHERE A.UniqueCode=?UniqueCode");
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?UniqueCode", uniqueCode, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<UserEntity>(sql, parameter);
        }
        public async Task<UserEntity> GetUserByPhone(string phone)
        {
            string sql = string.Concat(QUERY, " WHERE A.Phone=?Phone");
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?Phone", phone, DbType.AnsiString, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<UserEntity>(sql, parameter);
        }

        /// <summary>
        /// 登陆并返回UnionId
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public async Task<UserEntity> GetUserWithUnionIdByPhone(string phone)
        {
            string sql = @"SELECT A.UserId,Phone,Password,UserName,A.CreateTime,LastLoginTime,CurrentlyLoginTime,IdentityCardNo,UserIntegrals,LogonIp,AddressIp,
                            Avatar,AccountStatus,ProvinceCode,CityCode,CountyCode,Role,PassCode,PassExpirationTime,C.UnionId,A.CompanyName,A.UniqueCode,A.EvaluationScore,A.IsReceiveRedPacket
                            FROM User A
                                    LEFT JOIN UserAuth B ON A.UserId = B.UserId
                                    LEFT JOIN WeChatUserInfo C ON C.Unionid=B.Unionid
                            WHERE A.Phone=?Phone";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?Phone", phone, DbType.AnsiString, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<UserEntity>(sql, parameter);
        }

        public async Task<UserEntity> GetUserByUserId(long userId)
        {
            string sql = string.Concat(QUERY, " WHERE A.UserId=?UserId");
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?UserId", userId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<UserEntity>(sql, parameter);
        }

        public async Task<(IEnumerable<WeChatUserInfoEntity>, int)> GetUserList(string userName, string phone, string identityNumber, AccountStatus? accountStatus, RoleType? roleType, DateTime? start, DateTime? end, bool? ascending, int pageIndex = 1, int pageSize = 10)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (accountStatus.HasValue)
            {
                wheres.Add("A.AccountStatus=?AccountStatus");
                parameters.Add("?AccountStatus", accountStatus.Value.ToString(), DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(userName))
            {
                wheres.Add("A.UserName=?UserName");
                parameters.Add("?UserName", userName, DbType.AnsiString, size: 20);
            }
            if (!string.IsNullOrWhiteSpace(phone))
            {
                wheres.Add("A.Phone=?Phone");
                parameters.Add("?Phone", phone, DbType.AnsiString, size: 20);
            }
            if (!string.IsNullOrWhiteSpace(identityNumber))
            {
                wheres.Add("A.IdentityCardNo=?IdentityCardNo");
                parameters.Add("?IdentityCardNo", identityNumber, DbType.AnsiString, size: 18);
            }

            if (roleType.HasValue)
            {
                wheres.Add("A.Role=?Role");
                parameters.Add("?Role", roleType.ToString(), DbType.AnsiString);
            }

            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("A.CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("A.CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("A.CreateTime>=?Start AND A.CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY A.UserId {4} LIMIT {2},{3};
                               SELECT Count(1) FROM User A {1}",
                  QUERY_UNIONID_VIEW,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");

            return await this.databaseContext.Slave.QueryMultipleAsync<WeChatUserInfoEntity, int>(sql, parameters);
        }

        public async Task<bool> UpdateAvatar(UserEntity userEntity)
        {
            string sql = @"UPDATE User SET
                                Avatar=?Avatar
                           WHERE UserId=?UserId";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?Avatar", userEntity.Avatar, DbType.AnsiString, size: 255);
            parameter.Add("?UserId", userEntity.UserId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameter, databaseContext.Transaction) > 0;
        }

        public async Task<int> UpdateVip(long userId)
        {
            string sql = @"UPDATE User SET
                                IsVip=true
                           WHERE UserId=?UserId";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?UserId", userId, DbType.Int64);
            return await databaseContext.Master.ExecuteAsync(sql, parameter, databaseContext.Transaction);
        }

        public void UpdateLoginInfo(string phone, string ip, string addressIp)
        {
            string sql = @"UPDATE User SET
                                LastLoginTime=CurrentlyLoginTime,
                                CurrentlyLoginTime=?CurrentlyLoginTime,
                                LogonIp=?LogonIp,
                                AddressIp=?AddressIp
                            WHERE Phone=?Phone";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?CurrentlyLoginTime", DateTime.Now, DbType.DateTime);
            parameter.Add("?Phone", phone, DbType.AnsiString, size: 20);
            parameter.Add("?LogonIp", ip, DbType.AnsiString, size: 50);
            parameter.Add("?AddressIp", addressIp, DbType.AnsiString, size: 50);
            this.databaseContext.Master.ExecuteAsync(sql, parameter, databaseContext.Transaction);
        }

        public async Task<bool> UpdatePassword(UserEntity userEntity)
        {
            string sql = @"UPDATE User SET
                                Password=?Password
                           WHERE UserId=?UserId";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?Password", userEntity.Password, DbType.AnsiString, size: 255);
            parameter.Add("?UserId", userEntity.UserId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameter, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdatePhone(UserEntity userEntity)
        {
            string sql = @"UPDATE User SET
                                Phone=?Phone,
                                Password=?Password
                           WHERE UserId=?UserId";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?UserId", userEntity.UserId, DbType.Int64, size: 20);
            parameter.Add("?Phone", userEntity.Phone, DbType.AnsiString, size: 20);
            parameter.Add("?Password", userEntity.Password, DbType.AnsiString, size: 255);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameter, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UserSignIn(UserEntity userEntity)
        {
            string sql = @"UPDATE User SET
                             UserIntegrals=?UserIntegrals
                           WHERE UserId=?UserId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserIntegrals", userEntity.UserIntegrals, DbType.Int32, size: 10);
            parameters.Add("?UserId", userEntity.UserId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<bool> UpdatePassCode(string phone, string passCode, DateTime passExpirationTime)
        {
            string sql = @"UPDATE User SET
                                PassCode=?PassCode,
                                PassExpirationTime=?PassExpirationTime
                           WHERE Phone=?Phone;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PassCode", passCode, DbType.AnsiString, size: 10);
            parameters.Add("?PassExpirationTime", passExpirationTime, DbType.DateTime);
            parameters.Add("?Phone", phone, DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        /// <summary>
        /// 修改用户昵称
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public async Task<UserEntity> UpdateUserName(long userId, string userName, string identityCardNo)
        {
            string updateSql = SqlCommands.GetUpdateSql("User", new string[] { "UserName", "IdentityCardNo" }, new string[] { "UserId" });
            string sql = string.Format("{0} {1} Where A.UserId=?UserId", updateSql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserId", userId, DbType.Int64);
            parameters.Add("?UserName", userName, DbType.AnsiString);
            parameters.Add("?IdentityCardNo", identityCardNo, DbType.AnsiString, size: 18);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<UserEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<UserEntity> UpdateUserInfo(UserEntity userEntity)
        {
            string sql = @"UPDATE User SET
                                Phone=?Phone,
                                Password=?Password,
                                UserName=?UserName,
                                IdentityCardNo=?IdentityCardNo,
                                Role=?Role,
                                ProvinceCode=?ProvinceCode,
                                CityCode=?CityCode,
                                CountyCode=?CountyCode,
                                IsVip=?IsVip,
                                IsEnterprise=?IsEnterprise,
                                CompanyName=?CompanyName
                            WHERE UserId=?UserId;
                            {0} WHERE A.UserId=?UserId;";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?UserId", userEntity.UserId, DbType.Int64, size: 20);
            parameter.Add("?Phone", userEntity.Phone, DbType.AnsiString, size: 20);
            parameter.Add("?Password", userEntity.Password, DbType.AnsiString, size: 255);
            parameter.Add("?UserName", userEntity.UserName, DbType.AnsiString, size: 20);
            parameter.Add("?IdentityCardNo", userEntity.IdentityCardNo, DbType.AnsiString, size: 18);
            parameter.Add("?Role", userEntity.Role.ToString(), DbType.AnsiString, size: 30);
            parameter.Add("?ProvinceCode", userEntity.ProvinceCode, DbType.AnsiString, size: 20);
            parameter.Add("?CityCode", userEntity.CityCode, DbType.AnsiString, size: 20);
            parameter.Add("?CountyCode", userEntity.CountyCode, DbType.AnsiString, size: 20);
            parameter.Add("?IsVip", userEntity.IsVip, DbType.Boolean);
            parameter.Add("?IsEnterprise", userEntity.IsEnterprise, DbType.Boolean);
            parameter.Add("?CompanyName", userEntity.CompanyName, DbType.AnsiString, size: 100);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<UserEntity>(string.Format(sql, QUERY), parameter, databaseContext.Transaction);
        }
        public async Task<int> UpdateAuthUserInfo(UserEntity userEntity)
        {
            string sql = @"UPDATE User SET
                                UserName=?UserName,
                                IdentityCardNo=?IdentityCardNo,
                                AccountStatus=?AccountStatus,
                                Role=?Role,
                                ProvinceId=?ProvinceId,
                                ProvinceCode=?ProvinceCode,
                                ProvinceName=?ProvinceName,
                                CityId=?CityId,
                                CityCode=?CityCode,
                                CityName=?CityName,
                                CountyId=?CountyId,
                                CountyCode=?CountyCode,
                                CountyName=?CountyName,
                                IsVip=?IsVip,
                                IsEnterprise=?IsEnterprise,
                                CompanyName=?CompanyName,
                                PartnerId=?PartnerId
                            WHERE UserId=?UserId;";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?UserId", userEntity.UserId, DbType.Int64, size: 20);
            parameter.Add("?Phone", userEntity.Phone, DbType.AnsiString, size: 20);
            parameter.Add("?Password", userEntity.Password, DbType.AnsiString, size: 255);
            parameter.Add("?UserName", userEntity.UserName, DbType.AnsiString, size: 20);
            parameter.Add("?IdentityCardNo", userEntity.IdentityCardNo, DbType.AnsiString, size: 18);
            parameter.Add("?AccountStatus", userEntity.AccountStatus.ToString(), DbType.AnsiString);
            parameter.Add("?Role", userEntity.Role, DbType.AnsiString, size: 30);
            parameter.Add("?ProvinceId", userEntity.ProvinceId, DbType.Int64);
            parameter.Add("?ProvinceCode", userEntity.ProvinceCode, DbType.AnsiString, size: 20);
            parameter.Add("?ProvinceName", userEntity.ProvinceName, DbType.AnsiString, size: 50);
            parameter.Add("?CityId", userEntity.CityId, DbType.Int64);
            parameter.Add("?CityCode", userEntity.CityCode, DbType.AnsiString, size: 20);
            parameter.Add("?CityName", userEntity.CityName, DbType.AnsiString, size: 50);
            parameter.Add("?CountyId", userEntity.CountyId, DbType.Int64);
            parameter.Add("?CountyCode", userEntity.CountyCode, DbType.AnsiString, size: 20);
            parameter.Add("?CountyName", userEntity.CountyName, DbType.AnsiString, size: 50);
            parameter.Add("?IsVip", userEntity.IsVip, DbType.Boolean);
            parameter.Add("?IsEnterprise", userEntity.IsEnterprise, DbType.Boolean);
            parameter.Add("?CompanyName", userEntity.CompanyName, DbType.AnsiString, size: 100);
            parameter.Add("?PartnerId", userEntity.PartnerId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(string.Format(sql, QUERY), parameter, databaseContext.Transaction);
        }

        public async Task<bool> CheckUserExistsByUniqueCode(string uniqueCode)
        {
            string sql = @"SELECT 1 FROM User WHERE UniqueCode=?UniqueCode;";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?UniqueCode", uniqueCode, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.ExecuteScalarAsync(string.Format(sql, QUERY), parameter) != null;
        }

        public async Task<bool> CheckUserId(long userId)
        {
            string sql = "SELECT 1 FROM User WHERE UserId=?UserId";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?UserId", userId);
            return await this.databaseContext.Slave.ExecuteScalarAsync(string.Format(sql, QUERY), parameter) != null;
        }

        public async Task<long> GetUserId(string uniqueCode)
        {
            string sql = @"SELECT UserId FROM User WHERE UniqueCode=?UniqueCode";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?UniqueCode", uniqueCode, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.ExecuteScalarAsync<long>(sql, parameter);

        }

        public async Task<int> UpdateAccountStatus(long userId, AccountStatus accountStatus)
        {
            string sql = SqlCommands.GetUpdateSql("User", new string[] { "AccountStatus" }, new string[] { "UserId" });
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?UserId", userId, DbType.Int64, size: 20);
            parameter.Add("?AccountStatus", accountStatus.ToString(), DbType.AnsiString);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameter, databaseContext.Transaction);
        }

        public async Task<IEnumerable<UserEntity>> GetUserList(RoleType roleType, AccountStatus accountStatus)
        {
            string sql = $"{QUERY} WHERE A.Role=?Role AND A.AccountStatus=?AccountStatus";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?Role", roleType.ToString(), DbType.AnsiString, size: 20);
            parameter.Add("?AccountStatus", accountStatus.ToString(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Slave.QueryAsync<UserEntity>(sql, parameter);
        }
        public async Task<int> ReceiveRedPacket(long userId)
        {
            string sql = $"UPDATE `user` SET IsReceiveRedPacket=1 WHERE UserId=?UserId";
            return await this.databaseContext.Master.ExecuteAsync(sql, new { userId }, databaseContext.Transaction);
        }

        public async Task<UserEntity> RealNameVerifiesAsync(UserEntity user)
        {
            string sql = SqlCommands.GetUpdateSql("User", new[] { "RealName", "IdentityCardNo", "IdentityCardPath", "IdentityCardBackPath", "ProvinceId", "ProvinceCode", "ProvinceName", "CityId", "CityCode", "CityName", "CountyId", "CountyCode", "CountyName" }, new[] { "UserId" });

            sql = string.Format("{0} {1} WHERE A.UserId=?UserId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserId", user.UserId, DbType.Int32, size: 20);
            parameters.Add("?RealName", user.RealName, DbType.AnsiString, size: 21);
            parameters.Add("?IdentityCardNo", user.IdentityCardNo, DbType.AnsiString, size: 1000);
            parameters.Add("?IdentityCardPath", user.IdentityCardPath, DbType.AnsiString, size: 1000);
            parameters.Add("?IdentityCardBackPath", user.IdentityCardPath, DbType.AnsiString, size: 1000);
            parameters.Add("?ProvinceId", user.ProvinceId, DbType.Int64);
            parameters.Add("?ProvinceCode", user.ProvinceCode, DbType.AnsiString, size: 20);
            parameters.Add("?ProvinceName", user.ProvinceName, DbType.AnsiString, size: 50);
            parameters.Add("?CityId", user.CityId, DbType.Int64);
            parameters.Add("?CityCode", user.CityCode, DbType.AnsiString, size: 20);
            parameters.Add("?CityName", user.CityName, DbType.AnsiString, size: 50);
            parameters.Add("?CountyId", user.CountyId, DbType.Int64);
            parameters.Add("?CountyCode", user.CountyCode, DbType.AnsiString, size: 20);
            parameters.Add("?CountyName", user.CountyName, DbType.AnsiString, size: 50);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<UserEntity>(sql, parameters, databaseContext.Transaction);
        }

        /// <summary>
        /// 实名认证审核
        /// </summary>
        /// <param name="entity">实名认证审核信息</param>
        /// <returns></returns>
        public async Task<UserEntity> RealNameVerifiesApproveAsync(UserEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("User", new[] { "RealNameVerifiesRefuse", "RealNameVerifiesStatus", "Approver", "ApproverId", "ApproveTime" }, new[] { "UserId" });
            sql = string.Format("{0} {1} WHERE A.UserId=?UserId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserId", entity.UserId, DbType.Int32, size: 20);
            parameters.Add("?RealNameVerifiesRefuse", entity.RealNameVerifiesRefuse, DbType.AnsiString);
            parameters.Add("?RealNameVerifiesStatus", entity.RealNameVerifiesStatus, DbType.AnsiString);
            parameters.Add("?Approver", entity.Approver, DbType.AnsiString, size: 20);
            parameters.Add("?ApproverId", entity.ApproverId, DbType.Int64);
            parameters.Add("?ApproveTime", entity.ApproveTime, DbType.DateTime);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<UserEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<bool> UpdateUserParent(long userId, long parentUserId)
        {
            string sql = SqlCommands.GetUpdateSql("User", new string[] { "ParentId" }, new string[] { "UserId" });
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?UserId", userId, DbType.Int64, size: 20);
            parameter.Add("?ParentId", parentUserId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameter, databaseContext.Transaction) > 0;
        }
    }
}
